package com.ymatou.iapi.productquery.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productquery.parameter.ProductHistory.ProductHistoryBean;
import com.ymatou.iapi.productquery.service.GetProductListByHistoryProductIdListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.HistoryProductModelWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_GetProductListByHistoryProductIdList {
	private static GetProductListByHistoryProductIdListCall getProductListByHistoryProductIdListCall = new GetProductListByHistoryProductIdListCall();
	private static HistoryProductModelWapper historyProductModelWapper = new HistoryProductModelWapper();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");	
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询历史商品列表");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductListByHistoryProductIdListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByHistoryProductIdList_001() {
		Logger.start(true,"商品在历史库中");
		try{
			Map<String,Object> prodInHis = historyProductModelWapper.getBySellerId(3383);
			String productId = prodInHis.get("ProductId").toString();
			checkHistoryByProdId(MapUtil.hashMap(productId, true));		
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByHistoryProductIdList_002() {
		Logger.start(true,"商品在现有库中");
		try{
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc2.json");
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));
			String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			checkHistoryByProdId(MapUtil.hashMap(productId, false));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByHistoryProductIdList_003() {
		Logger.start(true,"一个商品在历史库中，一个商品在现有库中");
		try{
			Map<String,Object> prodInHis = historyProductModelWapper.getBySellerId(3383);
			String productId1 = prodInHis.get("ProductId").toString();
			
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc2.json");
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));
			String productId2 = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			checkHistoryByProdId(MapUtil.hashMap(productId1, true, productId2, false));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	public static void checkHistoryByProdId(Map<String,Boolean> ProductAndIsFromHisTbl) throws Exception{
		//期待值 数据库中提取
		List<String> prodList = new ArrayList<>();
		
		
		List<ProductHistoryBean> tgtProductHistoryBeanList = new ArrayList<>();
		ProductAndIsFromHisTbl.forEach((x,y)->{
			try {
				prodList.add(x);
				tgtProductHistoryBeanList.add(ProductQueryCallService.getProductHistoryBean(x, y));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		
		
		JSONObject ret = getProductListByHistoryProductIdListCall.search(prodList);
		String chkdesc = "GetProductListByHistoryProductIdList";
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		
		//返回值取数据
		List<ProductHistoryBean> actProductHistoryBeanList = new ArrayList<>();
		JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("ProductList");
		for(int i=0;i<prodListAry.length();i++){
			actProductHistoryBeanList.add(ProductQueryCallService.getBeanFromJson(prodListAry.getJSONObject(i),ProductHistoryBean.class));
		}
		
		ProductQueryCallService.compareProductHistoryBean(prodList,tgtProductHistoryBeanList, actProductHistoryBeanList, chkdesc);
		
	}
	
	
}
